package com.xianbeiying.app.ckgl.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.loopj.android.http.TextHttpResponseHandler;
import com.xianbeiying.app.ckgl.MyApplication;
import com.xianbeiying.app.ckgl.R;
import com.xianbeiying.app.ckgl.bean.CollectBean;
import com.xianbeiying.app.ckgl.bean.JsonBean;
import com.xianbeiying.app.ckgl.http.HttpCallBackUtil;
import com.xianbeiying.app.ckgl.http.HttpPostManger;
import com.xianbeiying.app.ckgl.model.SetModel;
import com.xianbeiying.app.ckgl.util.LogUtils;
import com.xianbeiying.app.ckgl.util.ToastUtil;

import cz.msebera.android.httpclient.Header;

/**
 * 植物简介
 */
public class CropDeatilActivity extends BaseActivity implements HttpCallBackUtil {
    private ImageView iv_title_left;
    private TextView tv_title;
    //图片
    private ImageView zw_img;
    //名称，别名,生长周期,适宜温度
    private TextView tv_cainame, tv_biename, tv_sczq, tv_sywd;
    //技术,管理,内容
    private TextView tv_zzjs, tv_wd, tv_wd_content, tv_gz, tv_gz_content;
    //    private GetModel getModel;
    private WebView webView;
    private ImageView iv_collect;
    private SetModel setModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_crop_deatil);
        initView();
    }

    private void initView() {
//        getModel = new GetModel(this);
//        getModel.addResponseListener(this);
        webView = (WebView) findViewById(R.id.webView);
        iv_collect = (ImageView) findViewById(R.id.iv_collect);
        iv_title_left = (ImageView) findViewById(R.id.iv_title_left);
        tv_title = (TextView) findViewById(R.id.tv_title);
        zw_img = (ImageView) findViewById(R.id.zw_img);
        tv_cainame = (TextView) findViewById(R.id.tv_cainame);
        tv_biename = (TextView) findViewById(R.id.tv_biename);
        tv_sczq = (TextView) findViewById(R.id.tv_sczq);
        tv_sywd = (TextView) findViewById(R.id.tv_sywd);
        tv_zzjs = (TextView) findViewById(R.id.tv_zzjs);
        tv_wd = (TextView) findViewById(R.id.tv_wd);
        tv_wd_content = (TextView) findViewById(R.id.tv_wd_content);
        tv_gz = (TextView) findViewById(R.id.tv_gz);
        tv_gz_content = (TextView) findViewById(R.id.tv_gz_content);
        initData();
        iv_title_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
    int CROP_ID,CROP_TYPE,plantId;
    private void initData() {
        setModel = new SetModel(this);
        setModel.addResponseListener(this);
        tv_title.setText("植物简介");
        CROP_ID =getIntent().getIntExtra("CROP_ID", 0);
         CROP_TYPE = getIntent().getIntExtra("CROP_TYPE", 0);
        plantId = getIntent().getIntExtra("plantId", 0);
//        getModel.PlantModelss("plantmodel",CROP_ID,CROP_TYPE,true);
//
//        //需要访问的网址
//        String url= UrlBean.PlantModel+"plantmodel";
//post访问需要提交的参数
//由于webView.postUrl(url, postData)中 postData类型为byte[] ，
//通过EncodingUtils.getBytes(data, charset)方法进行转换
//        webView.postUrl(url+postDate, null);
//        LogUtils.e("0000",url);
//        webView.loadUrl(url);

//        webView.getSettings().setJavaScriptEnabled(true);
//        webView.requestFocus();
//        webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
//        webView.postUrl(url, EncodingUtils.getBytes("", "base64"));

        iv_collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setModel.ADDCOLLECTPlant("COLLECT_PLANTMODEL", CROP_ID, CROP_TYPE, plantId, MyApplication.loginBean.getUSERID(), true);
            }
        });
        setHtml();
    }

    private void setHtml() {
        String postDate = JsonBean.plantString(CROP_ID, CROP_TYPE, plantId);
        HttpPostManger.post(CropDeatilActivity.this, "http://"+MyApplication.IP+":8080/CloudIIMS/crop/detail", postDate, new TextHttpResponseHandler() {
            //获取成功
            @Override
            public void onSuccess(int i, Header[] headers, String s) {
                Log.e("种植模型", "加载成功" + "======" + i);
//                webView.getSettings().setJavaScriptEnabled(true);//是否支持动态加载js
//                webView.getSettings().setDefaultTextEncodingName("UTF-8");//编码方式
//                //以下两行代码是使网页能够自适屏
//                webView.getSettings().setUseWideViewPort(true);
//                webView.getSettings().setLoadWithOverviewMode(true);
//                webView.loadData(s, "text/html;charset=UTF-8", null);//加载网页
//                webView.loadDataWithBaseURL(null, s, "text/html",
//                        "utf-8", null);
                webView.getSettings().setDomStorageEnabled(true);//有可能是DOM储存API没有打开
                webView.getSettings().setBlockNetworkImage(false);
                webView.getSettings().setUseWideViewPort(false);
                webView.getSettings().setJavaScriptEnabled(true);
                webView.getSettings().setAllowFileAccess(true);
                webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
                webView.getSettings().setLoadsImagesAutomatically(true);////支持自动加载图片
                webView.loadDataWithBaseURL("http://"+ MyApplication.IP+":8080/", s, "text/html", "utf-8", null);
                webView.setWebViewClient(new WebViewClient(){
                    @Override
                    public boolean shouldOverrideUrlLoading(WebView view, String url) {
                        // TODO Auto-generated method stub
                        LogUtils.e("ppppppp",url);
                        //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
//                        view.loadDataWithBaseURL("http://"+ MyApplication.IP+":8080/",url, "text/html", "utf-8", null);
                        getContent(url,view);
                        return true;
                    }
                });
            }
            //获取失败
            @Override
            public void onFailure(int i, Header[] headers, String s, Throwable throwable) {
                Log.e("种植模型", "加载失败");
            }
        });
    }

    CollectBean  collectBean;
    @Override
    public void onCallBackData(String url, Object content) {
        collectBean= JSON.parseObject(new String((byte []) content),CollectBean.class);
        if(collectBean.isR()){
            iv_collect.setSelected(true);
            ToastUtil.showInfo(CropDeatilActivity.this,"收藏成功");
        }else{
            ToastUtil.showInfo(CropDeatilActivity.this,collectBean.getERR_MSG());
        }
    }
    private  void getContent(String url,final WebView mmwebView){
        HttpPostManger.get(url,new TextHttpResponseHandler() {
            //获取成功
            @Override
            public void onSuccess(int i, Header[] headers, String s){
                LogUtils.e("种植模型","加载成功"+i+"======");
                mmwebView.getSettings().setDomStorageEnabled(true);//有可能是DOM储存API没有打开
                mmwebView.getSettings().setBlockNetworkImage(false);
                mmwebView.getSettings().setUseWideViewPort(false);
                mmwebView.getSettings().setJavaScriptEnabled(true);
                mmwebView.getSettings().setAllowFileAccess(true);
                mmwebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
                mmwebView.getSettings().setLoadsImagesAutomatically(true);////支持自动加载图片
                mmwebView.loadDataWithBaseURL("http://"+ MyApplication.IP+":8080/", s, "text/html", "utf-8", null);
//                WebSettings webSettings = webView.getSettings();
//                //设置WebView属性，能够执行Javascript脚本
//                webSettings.setJavaScriptEnabled(true);
//                //设置可以访问文件
//                webSettings.setAllowFileAccess(true);
//                //设置支持缩放
//                webSettings.setBuiltInZoomControls(true);
//                //加载需要显示的网页
//                webView.loadUrl(s);
//                //设置Web视图
//                webView.setWebViewClient(new webViewClient ());
                LogUtils.e("种植模型","成功"+s+"======");
            }

            //获取失败
            @Override
            public void onFailure(int i, Header[] headers, String s, Throwable throwable) {
                Log.e("种植模型", "加载失败");
            }
        });
    }
    //改写物理按键——返回的逻辑
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if(keyCode==KeyEvent.KEYCODE_BACK)
        {
            if(webView.canGoBack())
            {
                webView.goBack();//返回上一页面
                setHtml();
                return true;
            }
            else
            {
                finish();
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
